﻿#include <GUID.HPP>

using namespace CxxlMan;

static char HexString[] = {'0','1','2','3','4','5','6','7','8','9',
                           'a','b','c','d','e','f'};

static wchar_t WHexString[] = {L'0',L'1',L'2',L'3',L'4',L'5',L'6',L'7',L'8',L'9',
                               L'a',L'b',L'c',L'd',L'e',L'f'};


/*******************************
**         Win32 的實作       **
*******************************/
#if (SysSelect == _WIN32_CxxlMan)

#include <windows.h>

CXXL_DLLEXPORT MB_String cxxlFASTCALL CxxlMan::CreateGuidStr()
{
  GUID guid;
  char temp[(sizeof(guid)*2 + 1)];

  CoCreateGuid(&guid);

  unsigned int i;
  for(i = 0; i < sizeof(guid); ++i)
  {
    unsigned char c = ((unsigned char *)&guid)[i];
    temp[i*2] = *(((c & 0xf0) >> 4)+ HexString);
    temp[i*2+1] = *((c & 0xf) + HexString);
  }
  temp[i*2] = '\0';

  return MB_String(temp);
}

CXXL_DLLEXPORT UNICODE_String cxxlFASTCALL CxxlMan::CreateGuidWStr()
{
  GUID guid;
  wchar_t temp[(sizeof(guid)*2 + 1)];

  CoCreateGuid(&guid);

  unsigned int i;
  for(i = 0; i < sizeof(guid); ++i)
  {
    unsigned char c = ((unsigned char *)&guid)[i];
    temp[i*2] = *(((c & 0xf0) >> 4)+ WHexString);
    temp[i*2+1] = *((c & 0xf) + WHexString);
  }
  temp[i*2] = L'\0';

  return UNICODE_String(temp);
}


/*******************************
**         Linux 的實作       **
*******************************/
#elif (SysSelect == _LINUX_CxxlMan)

#include <uuid/uuid.h>

CXXL_DLLEXPORT MB_String cxxlFASTCALL CxxlMan::CreateGuidStr()
{
  uuid_t guid;
  char temp[(sizeof(guid)*2 + 1)];

  uuid_generate(guid);
  unsigned int i;
  for(i = 0; i < sizeof(guid); ++i)
  {
    unsigned char c = ((unsigned char *)guid)[i];
    temp[i*2] = *(((c & 0xf0) >> 4)+ HexString);
    temp[i*2+1] = *((c & 0xf) + HexString);
  }
  temp[i*2] = '\0';

  return MB_String(temp);
}

CXXL_DLLEXPORT UNICODE_String cxxlFASTCALL CxxlMan::CreateGuidWStr()
{
  uuid_t guid;
  wchar_t temp[(sizeof(guid)*2 + 1)];

  uuid_generate(guid);
  unsigned int i;
  for(i = 0; i < sizeof(guid); ++i)
  {
    unsigned char c = ((unsigned char *)guid)[i];
    temp[i*2] = *(((c & 0xf0) >> 4)+ WHexString);
    temp[i*2+1] = *((c & 0xf) + WHexString);
  }
  temp[i*2] = L'\0';

  return UNICODE_String(temp);
}


#else
  #error 尚未支援此作業系統
#endif
